﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using DiamondDash.Core;
using Microsoft.Phone.Tasks;

namespace DiamondDash.Pages
{
    /// <summary>
    /// 准备进行游戏页面，进行道具购买
    /// </summary>
    public partial class ReadyPage : PhoneApplicationPage
    {
        public static ReadyPage Instance = null;

        public ReadyPage()
        {
            InitializeComponent();
            Instance = this;
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            this.levelProgress.SetLevelProgress(StageManager.Instance.CurrLevel, StageManager.Instance.GetLevelExpPercent());
            this.txtCoinCount.Text = StageManager.Instance.TotalG.ToString();
            this.txtHighScore.Text = StageManager.Instance.HighScore.ToString();

            //游戏道具管理
            this.toolBomb.Init();
            this.toolColor.Init();
            this.toolTime.Init();
            this.toolBomb.SetCount(StageManager.Instance.BombCount);
            this.toolColor.SetCount(StageManager.Instance.ColorCount);
            this.toolTime.SetCount(StageManager.Instance.TimeCount);
            this.toolBomb.SetLock(true, "LEVEL 2");
            this.toolTime.SetLock(true, "LEVEL 6");
            this.toolColor.SetLock(true, "LEVEL 10");

            //2级开放炸弹，6级开放时间，10级开放颜色
            if (StageManager.Instance.CurrLevel >= 2)
            {
                this.toolBomb.SetLock(false);
                if (StageManager.Instance.IsFirstUnlockBomb)
                {
                    StageManager.Instance.IsFirstUnlockBomb = false;
                    //送一个道具
                    Dispatcher.BeginInvoke(() =>
                    {
                        MessageBox.Show("恭喜你解锁了超级炸弹道具，快去试试吧！");
                        StageManager.Instance.BombCount += 1;
                        this.toolBomb.SetCount(StageManager.Instance.BombCount);
                    });
                }
            }
            if (StageManager.Instance.CurrLevel >= 6)
            {
                this.toolTime.SetLock(false);
                if (StageManager.Instance.IsFirstUnlockTime)
                {
                    StageManager.Instance.IsFirstUnlockTime = false;
                    //送一个道具
                    Dispatcher.BeginInvoke(() =>
                    {
                        MessageBox.Show("恭喜你解锁了时间机器道具，快去试试吧！");
                        StageManager.Instance.TimeCount += 1;
                        this.toolTime.SetCount(StageManager.Instance.TimeCount);
                    });
                }
            }
            if (StageManager.Instance.CurrLevel >= 10)
            {
                this.toolColor.SetLock(false);
                if (StageManager.Instance.IsFirstUnlockColor)
                {
                    StageManager.Instance.IsFirstUnlockColor = false;
                    //送一个道具
                    Dispatcher.BeginInvoke(() =>
                    {
                        MessageBox.Show("恭喜你解锁了颜色画板道具，快去试试吧！");
                        StageManager.Instance.ColorCount += 1;
                        this.toolColor.SetCount(StageManager.Instance.ColorCount);
                    });
                }
            }

            if (StageManager.Instance.IsPlaySound)
            {
                this.btnSoundOn.Visibility = System.Windows.Visibility.Visible;
                this.btnSoundOff.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                this.btnSoundOn.Visibility = System.Windows.Visibility.Collapsed;
                this.btnSoundOff.Visibility = System.Windows.Visibility.Visible;
            }

            GotoMarketToComment();
        }

        private void btnStart_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            //进入游戏界面，开始进行游戏

            SoundManager.PlayEffect(SoundManager.clickButton);
            this.NavigationService.Navigate(new Uri("/Pages/GamePage.xaml", UriKind.RelativeOrAbsolute));

        }

        public void RefreshUI()
        {
            this.toolBomb.SetCount(StageManager.Instance.BombCount);
            this.toolColor.SetCount(StageManager.Instance.ColorCount);
            this.toolTime.SetCount(StageManager.Instance.TimeCount);

            this.txtCoinCount.Text = StageManager.Instance.TotalG.ToString();
        }

        private void btnSoundOn_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            //关闭音效
            StageManager.Instance.IsPlaySound = false;
            this.btnSoundOn.Visibility = System.Windows.Visibility.Collapsed;
            this.btnSoundOff.Visibility = System.Windows.Visibility.Visible;
        }

        private void btnSoundOff_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            //打开音效
            StageManager.Instance.IsPlaySound = true;
            this.btnSoundOn.Visibility = System.Windows.Visibility.Visible;
            this.btnSoundOff.Visibility = System.Windows.Visibility.Collapsed;
        }

        /// <summary>
        /// 去市场评价
        /// </summary>
        private void GotoMarketToComment()
        {
            if (!StageManager.Instance.IsCommented && StageManager.Instance.LaunchCount > 15)
            {
                if (MessageBox.Show("喜欢我们的游戏吗？请给我们一个五星好评吧，谢谢您的支持", "去评分", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    MarketplaceReviewTask marketplaceReviewTask = new MarketplaceReviewTask();
                    marketplaceReviewTask.Show();
                    StageManager.Instance.IsCommented = true;
                }
                else
                {
                    StageManager.Instance.LaunchCount = StageManager.Instance.LaunchCount + 1;
                    StageManager.Instance.IsCommented = true;
                }
            }
        }
    }
}